'''
Created on Aug 13, 2012

@author: petrina
'''
 
import logging

class Processor(object):
    '''
    This class manages the processing (i.e. calculation of indicators).
    
    Therefore it takes the indicator-configuration, inits and sets up the indicator_list.
    It offers methods for calculating the indicators (if indiconf is None all 
    indicators are calculated).
    '''


    def __init__(self, indiset = None):
        '''
        The constructor builds indicator-list using the given link object.
        
        The calculation of the indicators is not started. 
        The method calculate_indicators() is responsible for the calculations.
        The indiconf will be passed to the set of indicators.
        
        @param indiconf: the Indicator-configuration is a list containing the indicators-abbreviations or None (= all indicators, default) 
        '''
        
        self._indiset = indiset
        self.calculate_indicators()
            
        logging.debug("Processor: created Processor")
        
        
    #----------------------public methods of the class-------------------------
    
    
    def calculate_indicators(self):
        '''
        calculates all indicators in the indicatorset.
        
        @return: reference to the indicator-set containing the indicators
        '''
        
        logging.info("Processor: start calculating the indicators")
        
        if self._indiset is not None:
            self._indiset.build_indis()
            
            for indi in self._indiset: 
                try:
                    indi.calculate()
                except Exception, e:
                    logging.error("Processor: cannot calculate the indicator " + indi.name + " because " + e.__str__())
            
        return self._indiset     